package com.nerve.cloudoffice.web.action;

import java.util.Date;

import org.nerve.util.DateUtil;

import com.nerve.cloudoffice.common.Reply;
import com.nerve.cloudoffice.common.util.JSONHelper;
import com.nerve.cloudoffice.domain.Task;
import com.nerve.cloudoffice.domain.TrashApply;
import com.nerve.cloudoffice.service.TaskService;
import com.nerve.cloudoffice.service.impl.TaskServiceImpl;
import com.nerve.cloudoffice.web.model.TaskModel;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @project: cloudOffice
 * @file: TaskAction.java
 * @package: com.nerve.cloudoffice.web.action
 * @description:
 *
 * @author: 集成显卡	1053214511@qq.com
				IBM GDC, zengxm@cn.ibm.com
 * @date&time: 2013-8-3
 * @change log:
 */
@SuppressWarnings("serial")
public class TaskAction extends NerveAction implements ModelDriven<TaskModel>{

	private TaskService service = new TaskServiceImpl();
	
	private TaskModel model = new TaskModel();

	public TaskModel getModel() {
		return model;
	}

	public void setModel(TaskModel model) {
		this.model = model;
	}
	
	/**
	 * @method name: add
	 * @description: 
	 * @return type: void
	 */
	public void add(){
		Reply re = new Reply();
		try{
			Date startD = DateUtil.toDate(model.getStartDate());
			Date endD = DateUtil.toDate(model.getEndDate());
			
			System.out.println(model.getTask().getStatus());
			
			model.getTask().setStartDate(startD);
			model.getTask().setEndDate(endD);
			
			service.save(model);
			
			re.setMessage(SUCCESS);
		}catch(Exception e){
			e.printStackTrace();
			
			re.setError(true);
			re.setMessage(e);
		}
		
		json(re);
	}
	
	/**
	 * @method name: accept
	 * @description: 修改任务的接受状态
	 * @return type: void
	 */
	public void accept(){
		Reply re = new Reply();
		
		try{
			service.accept(model);
			re.setMessage(SUCCESS);
		}catch(Exception e){
			re.error(e);
			System.out.println(e.getMessage());
		}
		
		json(re);
	}
	
	
	/**
	 * @method name: list
	 * @description: 
	 * @return type: void
	 */
	public void list(){
		Reply re = new Reply();
		try{
			service.list(model);
			
			re.setData(model.getTotalCount());
			re.setMessage(model.getTaskList());
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("------------------ERRPR================");
			re.setError(true);
			re.setMessage(e);
		}
		
		json(re, JSONHelper.getValueFilter(Task.class));
	}
	
	/**
	 * @method name: update
	 * @description: 
	 * @return type: void
	 */
	public void update(){
		Reply re = new Reply();
		try{
			service.updateTask(model);
			
			re.setMessage(SUCCESS);
		}catch(Exception e){
			e.printStackTrace();
			
			re.setError(true);
			re.setMessage(e);
		}
		
		json(re);
	}
	
	public void updateBatch(){
		Reply re = new Reply();
		try{
			service.updateBatch(model);
			re.setMessage(SUCCESS);
		}catch(Exception e){
			e.printStackTrace();
			
			re.setError(true);
			re.setMessage(e);
		}
		
		json(re);
	}
	
	/**
	 * @method name: trash
	 * @description: 删除一个任务，这个操作是在同意“删除申请”后执行，传递的id值为TrashApply的id
	 * @return type: void
	 */
	public void passTrashApply(){
		Reply re = new Reply();
		try{
			service.trash(model);
			
			re.setMessage(SUCCESS);
		}catch(Exception e){
			e.printStackTrace();
			
			re.setError(true);
			re.setMessage(e);
		}
		
		json(re);
	}
	
	public void applyList(){
		Reply re = new Reply();
		try{
			service.listTrash(model);
			
			re.setData(model.getTotalCount());
			re.setMessage(model.getTrashList());
		}catch(Exception e){
			e.printStackTrace();
			re.setError(true);
			re.setMessage(e);
		}
		
		json(re, JSONHelper.getValueFilter(TrashApply.class));
	}
}